// JavaScript Document
$(function(){
	getRoomInfo();
	
	document.onkeydown=function(e){
		if(e.keyCode==13){
			sendMsg();
			$("#send_message_textarea").focus();
			e.returnValue = false;
			return false;
		}
	}
});

/*获取房间信息*/
function getRoomInfo(){
	var rooms=$("#roomsinfo li");
	var room_num=0;
	var current_num=0;
	var max_num=0;
	$.each(rooms,function(i){
		$(rooms[i]).click(function(){
			$(".no_chat").css({display:"none",zIndex:"100"}).animate({opacity:"0"},10);
			$(".start_chat").css({display:"block"}).animate({opacity:"1"},1000);
			
			room_num=$(this).find("span.room_num").text();
			current_num=$(this).find("span.current_num").text();
			max_num=$(this).find("span.max_num").text();
			
			$("#room_num").text("房间号："+room_num);
			$("#current_num").text("房间人数："+current_num+"人");
			$("#max_num").text("人数上限："+max_num+"人");
		});
	});
}

/*发送消息*/
function sendMsg(){
	var msg=$("#send_message_textarea").val();
	var msg_win=$("#message_win_show");
	var uname="张三";
	var time=new Date().toLocaleString();
	var text="";
	
	if(msg=="" || msg==null){
		alert("消息不能为空!");
		return false;
	}else{
		text=msg_win.html()+'<div id="one_msg"><p><span id="uanme">'+uname+'</span><span id="time">'+time+'</span></p><span id="msg_content">'
			+msg+'</span><hr id="msg_bottom_hr" style="1px dashed #ccc"/>';
		msg_win.html(text);
		var msg=$("#send_message_textarea").val("");
	}
}

/*取消发送*/
function cancelMsg(){
	var text=$("#send_message_textarea");
	text.val("");
	text.select();
}

/*清除聊天窗口信息*/
function clearMsg(){
	$("#message_win_show").html("");
}

/*退出房间*/
function exitRoom(){
	$(".start_chat").animate({opacity:"0"},300);
	window.setTimeout(function(){
		$(".no_chat").css({display:"block",zIndex:"1000"}).animate({opacity:"1"},300);
	},300);
}

